<template>
  <div class="home">
    <div class="recent-visitor">
      <div class="recent-visitor-title">最近访客 ></div>
      <div class="recent-visitor-list">
        <div class="recent-visitor-item" v-for="i in 3" :key="i"></div>
      </div>
    </div>

    <div class="publish-moments">
      <div>
        <el-input type="textarea" placeholder="请输入内容" v-model="form.moment" />
      </div>
      <div class="publish-footer">
        <div class="publish-footer-left">
          <div class="publish-tab-item active">最新</div>
          <div class="publish-tab-item">公告</div>
        </div>
        <div class="publish-footer-right">
          <label for="file1">
            <i class="el-icon-picture-outline icon-picture"></i>
            <input @change="choosePicture" multiple hidden id="file1" type="file" />
          </label>
          <label for="file2">
            <i class="el-icon-video-camera icon-camera"></i>
            <input hidden id="file2" type="file" />
          </label>
          <el-button type="primary" @click="publishMoment">发布</el-button>
        </div>
      </div>
    </div>

    <div class="moment-list" ref="momentList">
      <div class="moment-wrap">
        <div ref="momentLeft" class="moment-left">
          <comp-moment v-for="item in leftList" :moment="item" :key="item.id" />
        </div>
        <div ref="momentRight" class="moment-right">
          <comp-moment v-for="item in rightList" :moment="item" :key="item.id" />
        </div>
        <div style="clear:both;"></div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core';
import MouseWheel from '@better-scroll/mouse-wheel';
import { Input, Button } from 'element-ui';
import moment from './moment.vue';
import axios from '../../helper/axios';
import throttle from '../../helper/throttle';

BScroll.use(MouseWheel);

export default {
  name: 'Home',
  components: {
    'el-input': Input,
    'el-button': Button,
    'comp-moment': moment,
  },
  data() {
    return {
      form: {
        moment: '',
        images: [],
        video: '',
      },
      leftList: [],
      rightList: [],
    };
  },
  mounted() {
    axios.get('/api/moment/list').then((res) => {
      this.scroller = new BScroll(this.$refs.momentList, { probeType: 3, mouseWheel: true });
      this.scroller.on('scroll', throttle(() => {}, 100));
      this.appendList(res.momentlist);
    });
  },
  methods: {
    choosePicture(e) {
      // eslint-disable-next-line
      const form = new FormData();

      e.target.files.forEach((_file) => {
        form.append(_file.name, _file);
      });

      axios({ url: '/api/upload', method: 'post', data: form }).then((res) => {
        this.form.images = res;
      });
    },
    publishMoment() {
      axios.post('/api/moment/create', this.form).then((res) => {
        this.dependList(res);
        this.form = {
          moment: '',
          images: [],
          video: '',
        };
      });
    },
    dependList(value) {
      const l = this.$refs.momentLeft;
      const r = this.$refs.momentRight;

      if (l.clientHeight > r.clientHeight) {
        this.rightList = [value].concat(this.rightList);
      } else {
        this.leftList = [value].concat(this.leftList);
      }

      this.$nextTick(() => {
        this.scroller.refresh();
      });
    },
    appendList(arr) {
      const l = this.$refs.momentLeft;
      const r = this.$refs.momentRight;

      if (!arr.length) return;

      if (l.clientHeight > r.clientHeight) {
        this.rightList.push(arr.shift());
      } else {
        this.leftList.push(arr.shift());
      }

      this.$nextTick(() => {
        this.appendList(arr);
        this.scroller.refresh();
      });
    },
  },
};
</script>

<style>
.home {
  margin-left: 20px;
  margin-right: 20px;
  width: calc(100vw - 300px);
}
.recent-visitor {
  display: flex;
  align-items: center;
  margin-top: 20px;
  height: 36px;
}
.recent-visitor-title {
  font-size: 14px;
  color: #333;
}
.recent-visitor-list {
  display: flex;
  margin-left: 10px;
}
.recent-visitor-item {
  width: 36px;
  height: 36px;
  background-color: #f5f5f5;
  border-radius: 5px;
  margin-left: 5px;
}

.publish-moments {
  margin-top: 20px;
  height: 104px;
}

.publish-footer {
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.publish-footer-left {
  display: flex;
  align-items: center;
}

.publish-tab-item {
  width: 70px;
  text-align: center;
  font-size: 14px;
  color: #333;
}

.publish-tab-item.active {
  font-weight: bold;
}

.publish-footer-right {
  display: flex;
  align-items: center;
}

.icon-picture {
  font-size: 23px;
  margin-right: 20px;
}

.icon-camera {
  font-size: 27px;
  margin-right: 30px;
}

.moment-list {
  margin-top: 15px;
  height: calc(100vh - 195px);
  overflow: hidden;
}

.moment-left {
  width: calc((100vw - 320px) / 2 - 8px);
  min-height: 10px;
  float: left;
}
.moment-right {
  width: calc((100vw - 320px) / 2 - 8px);
  margin-left: 15px;
  min-height: 10px;
  float: left;
}
</style>
